package com.suning.yace;

import java.util.UUID;

import com.alibaba.fastjson.JSONObject;

public class RequestUtils {
	
	//消息类型
    public static final String REQUEST_TYPE = "type";
	
	   //SessionId
	public static final String SESSION_ID = "session_id";
	
	//握手
	public interface HandShakeRequestKey {
		//会员编号
		String CUST_NUM = "custNum";
		//Token
		String TOKEN = "token";
		//设备号
		String DEVICE_ID = "deviceId";
		//心跳时间
		String HEATBEAT = "heartbeart_interval";
	}
	
	//加入房间
	public interface JoinRoomRequestKey {
		//房间ID
		String ROOM_ID = "roomId";
	}
	
	//退出房间
	public interface OutRoomRequestKey {
		//房间ID
		String ROOM_ID = "roomId";
	}
	
	//发送聊天室消息
	public interface ChatMessageRequestKey {
		//房间ID
		String TO_ROOM_ID = "toChatRoomId";
		//来源会员编号
		String FROM_CUST_NUM = "fromCustNum";
	    String FROM_NICK_NAME = "fromNickName";
	    String FROM_HEAD_PIC = "fromHeadPic";
		//内容
		String CONTENT = "content";
		
		String MSG_TYPE = "msgType";
		//消息等级
		String LEVEL = "level";
	}
	
	private enum Type {
		HS,JC,OC,C,HB,Praise
	}
	
	public enum MsgType {
	    text,voice,image,imgtxt
	}
	
	
	private static String getSessionId() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	// {"custNum":"custNum","deviceId":"deviceId","heartbeart_interval":20,"type":"HS","token":"token","session_id":"cd68fac9dd774819b6d9398284b635d3"}
	public static JSONObject getHandShakeRequest(String custNum, String deviceId, int hbInterval, String token) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put(REQUEST_TYPE, Type.HS.name());
    	jsonObject.put(HandShakeRequestKey.CUST_NUM, custNum);
    	jsonObject.put(HandShakeRequestKey.TOKEN, token);
    	jsonObject.put(HandShakeRequestKey.DEVICE_ID, deviceId);
    	jsonObject.put(HandShakeRequestKey.HEATBEAT, hbInterval);
    	jsonObject.put(SESSION_ID, getSessionId());
		return jsonObject;
	}
	
	//{"type":"JC","roomId":"roomId","session_id":"cd68fac9dd774819b6d9398284b635d3"}
	public static JSONObject getJoinRoomRequest(String roomId) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put(REQUEST_TYPE, Type.JC.name());
		jsonObject.put(JoinRoomRequestKey.ROOM_ID, roomId);
		jsonObject.put(SESSION_ID, getSessionId());
		return jsonObject;
	}
	
	//{"type":"OC","roomId":"roomId","session_id":"b9ff9bab851d4333aaaac0fb86c76d0c"}
	public static JSONObject getOutRoomRequest(String roomId) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put(REQUEST_TYPE, Type.OC.name());
		jsonObject.put(OutRoomRequestKey.ROOM_ID, roomId);
		jsonObject.put(SESSION_ID, getSessionId());
		return jsonObject;
	}
	
	//{"fromCustNum":"fromCustNum","type":"C","level":2,"content":"content","toChatRoomId":"roomId","session_id":"d2aef0470da54fe29fffc99cc7e8a8e3"}
    public static JSONObject chatMsgRequest(String toChatRoomId, String fromCustNum,String fromNickName, String headPic, String content,MsgType msgType) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(REQUEST_TYPE, Type.C.name());
        jsonObject.put(ChatMessageRequestKey.TO_ROOM_ID, toChatRoomId);
        jsonObject.put(ChatMessageRequestKey.FROM_CUST_NUM, fromCustNum);
        jsonObject.put(ChatMessageRequestKey.CONTENT, content);
        jsonObject.put(ChatMessageRequestKey.MSG_TYPE, msgType.name());
        jsonObject.put(ChatMessageRequestKey.LEVEL, 1);
        jsonObject.put(ChatMessageRequestKey.FROM_NICK_NAME, fromNickName);
        jsonObject.put(ChatMessageRequestKey.FROM_HEAD_PIC, headPic);
        jsonObject.put(SESSION_ID, getSessionId());
        return jsonObject;
    }
	
	//{"type":"HB","session_id":"1"}
	public static JSONObject getHeartBeatRequest() {
		return heartBeatRequestStr;
	}
	
	//{"type":"Praise", "session_id":"2"}
	public static JSONObject getPraiseRequest() {
		return praiseRequestStr;
	}
	
	private static final JSONObject heartBeatRequestStr;
	
	private static final JSONObject praiseRequestStr;
	
	static {
		heartBeatRequestStr = new JSONObject();
		heartBeatRequestStr.put(REQUEST_TYPE, Type.HB.name());
		heartBeatRequestStr.put(SESSION_ID, 1);
		
		praiseRequestStr = new JSONObject();
		praiseRequestStr.put(REQUEST_TYPE, Type.Praise.name());
		heartBeatRequestStr.put(SESSION_ID, 2);
	}
	
}
